/******************************************************************************

	COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2010.

	This file is part of Maxsi Client.

	Maxsi Client is free software: you can redistribute it and/or modify it
	under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or (at your
	option) any later version.

	Maxsi Client is distributed in the hope that it will be useful, but WITHOUT
	ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
	FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
	License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with Maxsi Client. If not, see <http://www.gnu.org/licenses/>.

	Maxsi Client
	A client UI to the digital distribution platform Maxsi Distribution.

	Main.cpp
	Handles the initialization of the UI and main loops.

******************************************************************************/

#include "MaxsiLibrary.h"
#include "Main.h"
#include "UI.h"
#include "PageClientMain.h"

MESTR*	ApplicationName			=	_MESTR("Maxsi Distribution");

MaxsiError OnEvent(MaxsiWindowEvent* Event, BYTE** UserData)
{
	Page**	WindowPagePointer	=	(Page**)UserData;
	Page*	WindowPage			=	*WindowPagePointer;

	if ( WindowPage )
	{
		MaxsiError	Result	=	WindowPage->OnEvent(Event);

		if ( WindowPage->ShouldDestroyPage )
		{
			if ( WindowPage->DeleteControls() != MAXSI_ERROR_SUCCESS )
			{
				WindowSystem->MsgBox(NULL, 0, Translate("MaxsiLibrary/MaxsiWindowCouldNotDeleteControls"), Translate("MaxsiClient/MaxsiClientWindowTitle"));
			}
			else
			{
				WindowPage->NextPage->SetWindow(WindowPage->Window);

				if ( WindowPage->NextPage->CreateControls() != MAXSI_ERROR_SUCCESS )
				{
					WindowSystem->MsgBox(NULL, 0, Translate("MaxsiLibrary/MaxsiWindowCouldNotCreateControls"), Translate("MaxsiClient/MaxsiClientWindowTitle"));
				}
				else
				{
					WindowPagePointer	=	&(WindowPage->NextPage);
				}
			}

			return Result;
		}
	}

	return MAXSI_ERROR_NOT_INITIALIZED;
}

int main(int argc, char** argv)
{
#if 0
	
	MaxsiHandle			FileHandle		=	FileSystem()->OpenFile(_MESTR("Test.mff"), MAXSI_FILE_WRITE);

	if ( FileHandle == NULL ) { printf("Could not open file!\n"); return 1; }

	MaxsiFileStreamer*	Stream			=	new MaxsiFileStreamer(FileSystem(), FileHandle);

	if ( Stream == NULL ) { printf("Could not create stream!\n"); return 1; }

	MaxsiFileFormat*	MFFFileSystem	=	new MaxsiFileFormat;

	if ( MFFFileSystem == NULL ) { printf("Could not create file system class!\n"); return 1; }

	if ( MFFFileSystem->SetDevice(Stream, MAXSI_FILE_READWRITE) != MAXSI_ERROR_SUCCESS ) { printf("Could not set file system device stream!\n"); return 1; }
	if ( MFFFileSystem->CreateFileSystem(16, 64, 256) != MAXSI_ERROR_SUCCESS ) { printf("Could not format file system\n"); return 1; }

	delete MFFFileSystem;
	delete Stream;

	FileSystem()->CloseFile(FileHandle);
#endif

	// Initialize the translation system.
	MaxsiError	ConfigurationResult		=	LoadMaxsiComponentConfiguration(_MESTR("MaxsiClient"));

	// Create the worker threads.
	if ( CreateWorkerThreadPool() != MAXSI_ERROR_SUCCESS )
	{
		CriticalMsgBox(NULL, 0, _MESTR("MaxsiLibrary/MaxsiWorkerCouldNotInitialize"), ApplicationName);
		return 1;	
	}

	// Get an instance of the local Maxsi Window System used on this platform.
	WindowSystem	=	(MaxsiWindowSystem*)CreateInterface("MaxsiWindowSystem");

	if ( WindowSystem == NULL )
	{
		CriticalMsgBox(NULL, 0, _MESTR("Could not create an instance of the Maxsi Window System."), ApplicationName);
		return 1;	
	}

	// Initialize the Maxsi Window System.
	if ( WindowSystem->Initialize(&argc, &argv) == 0 )
	{
		CriticalMsgBox(NULL, 0, _MESTR("Could not intialize the Maxsi Window System."), ApplicationName);	
		DeleteInterface(WindowSystem);	
		return 1;
	}

	if ( ConfigurationResult != MAXSI_ERROR_SUCCESS )
	{		
		WindowSystem->MsgBox(NULL, 0, Translate("MaxsiLibrary/MaxsiTranslateErrorLoadingConfiguration"), Translate("MaxsiLibrary/MaxsiTranslateErrorHeader"));
	}

	// Create the main Window
	MainWindow	=	WindowSystem->CreateWindow(NULL, _MESTR("Window"), 320, 650, MAXSI_WINDOW_CENTER_X, MAXSI_WINDOW_CENTER_Y, ApplicationName);

	if ( MainWindow == NULL )
	{
		CriticalMsgBox(NULL, 0, _MESTR("Could not create the main window."), ApplicationName);	
		DeleteInterface(WindowSystem);	
		return 1;
	}

	Page* NewPage	=	new PageClientMain;

	if ( NewPage == NULL )
	{
		CriticalMsgBox(NULL, 0, _MESTR("Could not create the main window page."), ApplicationName);	
		WindowSystem->DeleteWindow(MainWindow);
		DeleteInterface(WindowSystem);	
		return 1;		
	}

	// Set up the callback function that handles all events on behalf of MainWindow.
	MainWindow->SetEventHandler(OnEvent, (BYTE*)NewPage);

	NewPage->SetWindow(MainWindow);

	// Call the function above that creates all the controls!
	if ( NewPage->CreateControls() != MAXSI_ERROR_SUCCESS )
	{
		WindowSystem->MsgBox(NULL, 0, Translate("MaxsiLibrary/MaxsiWindowCouldNotCreateControls"), Translate("MaxsiClient/MaxsiClientWindowTitle"));	
		WindowSystem->DeleteWindow(MainWindow);
		DeleteInterface(WindowSystem);	
		return 1;	
	}

	// Now that we are done adding controls, it is time to show the window.
	MainWindow->Show();

	// Run the main event loop.
	int	Result = WindowSystem->MainLoop();

	// Clean up!	
	WindowSystem->DeleteWindow(MainWindow);
	DeleteInterface(WindowSystem);

	return Result;
}


